ostree/dump: Improve formatting for well-known commit metadata keys
authorPhilip Withnall <withnall@endlessm.com>
Wed, 7 Jun 2017 11:03:57 +0000 (12:03 +0100)
committerAtomic Bot <atomic-devel@projectatomic.io>
Wed, 7 Jun 2017 16:55:35 +0000 (16:55 +0000)
This follows on from commit a946c3d4, which added formatting for
well-known summary metadata keys. This commit adds it for commits.

Currently, the only well-known commit metadata key is
ostree.commit.timestamp. Formatting this correctly is especially
important, since it’s a big-endian uint64, which is completely unusable
for mere mortals when presented as a number rather than a date.

Non-formatted output can still be retrieved using the OSTREE_DUMP_RAW
flag, and the non-formatted key name is always printed for clarity.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
Closes: #911
Approved by: cgwalters

src/ostree/ot-dump.c
tests/test-summary-view.sh

index b24003c73a8bfadc4391d7cbeebe6a48084da0d0..e6b8859b6de0cb5df5de27ee7386acc10751cc4e 100644 (file)
@@ -71,6 +71,18 @@ format_timestamp (guint64  timestamp,
   return str;
 }
 
+static gchar *
+uint64_secs_to_iso8601 (guint64 secs)
+{
+  g_autoptr(GDateTime) dt = g_date_time_new_from_unix_utc (secs);
+  g_autoptr(GDateTime) local = (dt != NULL) ? g_date_time_to_local (dt) : NULL;
+
+  if (local != NULL)
+    return g_date_time_format (local, "%FT%T%:::z");
+  else
+    return g_strdup ("invalid");
+}
+
 static void
 dump_indented_lines (const gchar *data)
 {
@@ -196,21 +208,25 @@ dump_summary_ref (const char   *ref_name,
 
   while (g_variant_iter_loop (metadata, "{sv}", &key, &value))
     {
-      g_autofree char *string = g_variant_print (value, FALSE);
-      g_print ("    %s: %s\n", key, string);
-    }
-}
+      g_autofree gchar *value_str = NULL;
+      const gchar *pretty_key = NULL;
 
-static gchar *
-uint64_secs_to_iso8601 (guint64 secs)
-{
-  g_autoptr(GDateTime) dt = g_date_time_new_from_unix_utc (secs);
-  g_autoptr(GDateTime) local = (dt != NULL) ? g_date_time_to_local (dt) : NULL;
+      if (g_strcmp0 (key, OSTREE_COMMIT_TIMESTAMP) == 0)
+        {
+          pretty_key = "Timestamp";
+          value_str = uint64_secs_to_iso8601 (GUINT64_FROM_BE (g_variant_get_uint64 (value)));
+        }
+      else
+        {
+          value_str = g_variant_print (value, FALSE);
+        }
 
-  if (local != NULL)
-    return g_date_time_format (local, "%FT%T%:::z");
-  else
-    return g_strdup ("invalid");
+      /* Print out. */
+      if (pretty_key != NULL)
+        g_print ("    %s (%s): %s\n", pretty_key, key, value_str);
+      else
+        g_print ("    %s: %s\n", key, value_str);
+    }
 }
 
 void
index 6dcfd0883c0529051e70eded06c034260584d0a5..5b52b6911f7ceb492f3d8e41dfe252c723464228 100755 (executable)
@@ -52,6 +52,7 @@ assert_file_has_content_literal summary.txt "* main"
 assert_file_has_content_literal summary.txt "* other"
 assert_file_has_content_literal summary.txt "ostree.summary.last-modified"
 assert_file_has_content_literal summary.txt "Static Deltas (ostree.static-deltas): {}"
+assert_file_has_content_literal summary.txt "Timestamp (ostree.commit.timestamp): "
 echo "ok view summary"
 
 # Check the summary can be viewed raw too.